
package com.ruibo.modules.quartz.service.impl;


import com.ruibo.modules.quartz.model.QuartzLog;
import com.ruibo.modules.quartz.repository.QuartzLogRepository;
import com.ruibo.modules.quartz.service.QuartzLogService;
import com.ruibo.modules.quartz.service.dto.QuartzLogDto;
import com.ruibo.modules.quartz.service.dto.QuartzLogQueryCriteria;
import com.ruibo.modules.quartz.service.mapstruct.QuartzLogMapper;
import com.ruibo.modules.utils.PageUtil;
import com.ruibo.modules.utils.QueryHelp;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author Sun Feng
 * @website https://el-admin.vip
 * @description 服务实现
 * @date 2020-07-16
 **/
@Service
@RequiredArgsConstructor
public class QuartzLogServiceImpl implements QuartzLogService {

    private final QuartzLogRepository quartzLogRepository;
    private final QuartzLogMapper quartzLogMapper;

    @Override
    public Map<String, Object> queryAll(QuartzLogQueryCriteria criteria, Pageable pageable) {
        Page<QuartzLog> page = quartzLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(quartzLogMapper::toDto));
    }

    @Override
    public List<QuartzLogDto> queryAll(QuartzLogQueryCriteria criteria) {
        return quartzLogMapper.toDto(quartzLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public QuartzLogDto findById(Long id) {
        QuartzLog quartzLog = quartzLogRepository.findById(id).orElseGet(QuartzLog::new);

        return quartzLogMapper.toDto(quartzLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QuartzLogDto create(QuartzLog resources) {
        return quartzLogMapper.toDto(quartzLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(QuartzLog resources) {
        QuartzLog quartzLog = quartzLogRepository.findById(resources.getId()).orElseGet(QuartzLog::new);

        quartzLog.copy(resources);
        quartzLogRepository.save(quartzLog);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            quartzLogRepository.deleteById(id);
        }
    }

}
